<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>省市经销商联动查询</title>
<!-- <script type="text/javascript" src="js/jquery.min.js"></script> -->
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.validate.min.js"></script>
<script type="text/javascript" src="../../../js/common/common.js"></script>
<script type="text/javascript" src="../../../js/common/wechatCommon.js"></script>
<!-- <script type="text/javascript" src="../../js/jquery-1.7.2-min.js"></script> -->
</head>
<script type="text/javascript">
	
var rootPath = getRootPath();
	//页面加载后异步加载省份数据
 $(document).ready(function(){
	 $.ajax({
		  headers:{'Cookie':document.cookie},
		  //url:'http://127.0.0.1:9013/pointActivity/api/v1/province',
		  url:rootPath + '/pointActivity/api/v1/province',
		  contentType:"application/json", 
		  type:"POST", 
		  success:function(json){
	        	for(var i in json){
	        		var regionCode=i;
	        		var regionName=json[i];
	        		$("#province").append("<option value='"+regionCode+"'>"+regionName+"</option>"); 
	        	}
	        }
	  });
	 
	 
	 //当选择省份后触发此事件
	 $("#province").change(function(){
		$("#city option:not(:first)").remove();
		var regionCode=$(this).val();
		if(regionCode!=""){
			$.ajax({
				//url:"http://127.0.0.1:9013/pointActivity/api/v1/city",
				headers:{'Cookie':document.cookie},
				url:rootPath + "/pointActivity/api/v1/city",
				data:"regionCode="+regionCode,
				type:"POST", 
				success:function(json){
					for(var a in json){
						var cityCode=a;
						var cityName=json[a];
						 $("#city").append("<option value='"+cityCode+"'>"+cityName+"</option>");  
					}
				}
			});
		}
	 });
	 
	 //城市名称发生改变触发事件
	 $("#city").change(function(){
		 $("#dealer option:not(:first)").remove();
		 var cityid=$(this).val();
		 if(cityid!=""){
			 $.ajax({
				 	headers:{'Cookie':document.cookie},
				 	//url:"http://127.0.0.1:9013/pointActivity/api/v1/dealer",
				 	url:rootPath + "/pointActivity/api/v1/dealer",
					data:"regionCode="+cityid,
					type:"POST", 
					success:function(json){
						for(var b in json){
							var cityCode=b;
							var cityName=json[b];
							 $("#dealer").append("<option value='"+cityCode+"'>"+cityName+"</option>");  
						}
					}
				});
		 }
	 });
	 
	
	//校验
	 $(function(){
			$("#form1").validate({
				rules:{
					province:{
						required:true,
					},
					city:{
						required:true,
					},
					dealerCode:{
						required:true,
					},
					reserveDate:{
						required:true,
						date:true,
					}
				},
				messages:{
					province:{
						required:"必须选择所在省份!",
					},
					city:{
						required:"必须选择所在城市!",
					},
					dealerCode:{
						required:"必须选择指定经销商!",
					},
					reserveDate:{
						required:"必须选择预约时间!",
					}
				},
				/* 重写错误显示消息方法,以alert方式弹出错误消息 */  
		        /* showErrors: function(errorMap, errorList) {  
		            var msg = "";  
		            $.each( errorList, function(i,v){  
		              msg += (v.message+"\r\n");  
		            });  
		            if(msg!=""){
		            	alert(msg);  
		            }
		        },   */
		        /* 失去焦点时不验证 */   
		       /*  onfocusout: false   */
			});
			
		});
	
	 //查询活动时间
	 $.ajax({
		headers:{'Cookie':document.cookie},
		//url:"http://127.0.0.1:9013/pointActivity/api/v1/getTime",
		url:rootPath + "/pointActivity/api/v1/getTime",
		type:"POST", 
		success:function(json){
			for(var d in json){
				var times=json[d];
				$("#time").append("<option value='"+times+"'>"+times+"</option>");  
			}
		}
	 });
	 
	 //预约按键点击事件
	 $("#butt1").click(function(){
		 $.ajax({
			 	headers:{'Cookie':document.cookie},
			 	//url:"http://127.0.0.1:9013/pointActivity/api/v1/reservation",
			 	url:rootPath + "/pointActivity/api/v1/reservation",
				type:"POST", 
				data:$('#form1').serialize(),
				success:function(date){
					if(date!="0"){
						alert("预约成功");
					}else{
						alert("预约失败，请稍后再试！");
					}
				}
		 })
		    
	 })
 })
 
 
</script>
<body>
<form id="form1">
  省：<select id="province" name="province">  
        <option value="">请选择...</option>  
    </select>  
    市：<select id="city" name="city">  
        <option value="">请选择...</option>  
    </select>  
    经销商：<select id="dealer" name="dealerCode">  
        <option value="">请选择...</option>  
    </select> 
  预约时间:<select id="time" name="reservedate">
  		<option value="">请选择...</option> 
  		<!-- <option value="2017-09-01 08:25:20">2017-09-01</option> -->
       </select>
    <input type="button" value="预约" id="butt1"/>
</form>
   
<form action="http://127.0.0.1:9013/pointActivity/api/v1/initialize" method="post">
 <input type="submit" value="初始化"/>
</form>
</body>
</html>